package com.tanguy.user.ui.me.adapter

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.azhong.ratingbar.RatingBar
import com.tanguy.user.R
import com.tanguy.user.base.ImageViewTang
import com.tanguy.user.base.JustAdapter
import com.tanguy.user.bean.EvaluateHotel
import com.tanguy.user.utlis.TimeUtils
import com.tanguy.user.utlis.extend.dp2px
import com.tanguy.user.utlis.extend.setText
import com.tanguy.user.utlis.extend.toTimeString
import org.jetbrains.anko.find


/**
 * Created by dab on 2017/11/10 0010 17:15
 * 酒店评价
 */
class EvaluateHotelAdapter(mData: ArrayList<EvaluateHotel>) : JustAdapter<EvaluateHotel>(mData, R.layout.item_evaluate) {
    override fun bind(itemView: View, data: EvaluateHotel, position: Int) {
        itemView.apply {

            find<ImageViewTang>(R.id.iv_portrait).setImageURI(data.showImg)
            setText(R.id.tv_name_number, data.name)
            setText(R.id.tv_time, data.addtime.toTimeString(TimeUtils.TIME_YYYY_MM_DD))
            setText(R.id.tv_msg, data.content)
            setText(R.id.tv_grade, data.score.toString())
            find<RatingBar>(R.id.rb_grade).star = data.score
            val img = data.img
            if (TextUtils.isEmpty(img)) {
                find<View>(R.id.view_images).visibility = View.GONE
                return
            }
            val list = img.replace("{", "").replace("'", "").replace("\"", "").replace("[", "").replace("]", "").replace("}", "").replace(",", "").split("img:")
            if (list.size <= 1) return
            val arrayList = list as ArrayList<String>
            arrayList.removeAt(0)
            find<View>(R.id.view_images).visibility = View.VISIBLE
            val linearLayout = find<LinearLayout>(R.id.layout_images)
            linearLayout.removeAllViews()
            for (i in 0 until arrayList.size) {
                val imageViewTang = getImageViewTang(linearLayout.context, arrayList[i])
                com.tanguy.user.utlis.extend.click(imageViewTang) {
                    onImageClick?.invoke(arrayList, i)
                }
                linearLayout.addView(imageViewTang)
            }
        }

    }


    var onImageClick: ((list: ArrayList<String>, position: Int) -> Unit)? = null
    private fun getImageViewTang(context: Context, url: String): ImageViewTang {
        val imageViewTang = ImageViewTang(context)
        imageViewTang.apply {
            setImageURI(url)
            setPadding(context.dp2px(7), 0, 0, 0)
            layoutParams = ViewGroup.LayoutParams(context.dp2px(76), context.dp2px(67))
        }
        return imageViewTang
    }
}